package amalgame.ventas;

import java.util.ArrayList;

//import amalgame.data.DataSQLiteHelper;
import amalgame.data.DatabaseManager;
import amalgame.entity.Actividad;
import amalgame.entity.Util;
//import amalgame.entity.db.Customer;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.database.SQLException;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
//import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Actividad {
	private static final String TAGS = null;
	private GridView grdOpciones;
	private ArrayList<MenuItems> arrListItems;
	private Context mContext;
	public int NOTIFICATION = android.R.string.yes;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		grdOpciones = (GridView) findViewById(R.id.gridopciones);

		mContext = getApplicationContext();
		Actividad.mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		showNotification();

		arrListItems = new ArrayList<MenuItems>();
		final String titulos[] = { getString(R.string.customer),
				getString(R.string.products),
				getString(R.string.customer_order),
				getString(R.string.categoryproducts),
				getString(R.string.stock), 
				getString(R.string.info)
		};

		MenuItems mitem = new MenuItems("", "");

		for (int x = 0; x < titulos.length; x++) {
			mitem = new MenuItems("", titulos[x]);
			arrListItems.add(mitem);
		}

		grdOpciones.setAdapter(new AdaptadorTitulares(this, arrListItems));

		grdOpciones.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {

				try {
					Toast.makeText(mContext,
							arrListItems.get(position).getTitulo(),
							Toast.LENGTH_SHORT).show();

					if (getString(R.string.customer) == arrListItems.get(
							position).getTitulo()) {
						Intent intent = new Intent();
						intent.setClass(mContext, CustomerActivity.class);
						startActivity(intent);
					}

					if (getString(R.string.products) == arrListItems.get(
							position).getTitulo()) {
						Intent intent = new Intent();
						intent.setClass(mContext, ProductActivity.class);
						startActivity(intent);
					}

					if (getString(R.string.customer_order) == arrListItems.get(
							position).getTitulo()) {
						Intent intent = new Intent();
						intent.setClass(mContext, OrdersActivity.class);
						startActivity(intent);
					}

					if (getString(R.string.categoryproducts) == arrListItems.get(
							position).getTitulo()) {
						Intent intent = new Intent();
						intent.setClass(mContext, CategoriesActivity.class);
						startActivity(intent);
					}
					
					if (getString(R.string.stock) == arrListItems.get(
							position).getTitulo()) {
						Intent intent = new Intent();
						intent.setClass(mContext, StockActivity.class);
						startActivity(intent);
					}
					
					if (getString(R.string.info) == arrListItems.get(position).getTitulo()) {
						Intent intent = new Intent();
						intent.setClass(mContext, AboutusActivity.class);
						startActivity(intent);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});

		try {
			DatabaseManager.init(mContext);
			Util.init(mContext);
		} catch (SQLException e) {
			Log.e(TAGS, "Database exception", e);
			return;
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return true;
	}

	public class AdaptadorTitulares extends BaseAdapter {
		Activity context;
		private ArrayList<MenuItems> arrayListMenuItems;

		AdaptadorTitulares(Activity context,
				ArrayList<MenuItems> arrayListMenuItems) {
			this.context = context;
			this.arrayListMenuItems = arrayListMenuItems;
		}

		public View getView(int position, View convertView, ViewGroup parent) {

			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			View gridView;
			if (convertView == null) {
				gridView = new View(context);
				gridView = inflater.inflate(R.layout.iconrow, null);

				TextView textView = (TextView) gridView
						.findViewById(R.id.iconrow_title);
				textView.setText(arrayListMenuItems.get(position).getTitulo());

				Drawable iconDrawable = null;
				if (getString(R.string.customer) == arrayListMenuItems.get(
						position).getTitulo()) {
					iconDrawable = context.getResources().getDrawable(
							R.drawable.customer_on);
				}
				if (getString(R.string.products) == arrayListMenuItems.get(
						position).getTitulo()) {
					iconDrawable = context.getResources().getDrawable(
							R.drawable.products_on);
				}
				if (getString(R.string.customer_order) == arrayListMenuItems
						.get(position).getTitulo()) {
					iconDrawable = context.getResources().getDrawable(
							R.drawable.customer_order_on);
				}
				
				if (getString(R.string.categoryproducts ) == arrayListMenuItems
						.get(position).getTitulo()) {
					iconDrawable = context.getResources().getDrawable(
							R.drawable.category_product_on);
				}
				
				if (getString(R.string.stock ) == arrayListMenuItems
						.get(position).getTitulo()) {
					iconDrawable = context.getResources().getDrawable(
							R.drawable.search);
				}

				if (getString(R.string.info ) == arrayListMenuItems
						.get(position).getTitulo()) {
					iconDrawable = context.getResources().getDrawable(
							R.drawable.info);
				}
				
				ImageView imageView = (ImageView) gridView
						.findViewById(R.id.iconrow_icon);

				imageView.setImageDrawable(iconDrawable);

			} else {
				gridView = (View) convertView;
			}

			return gridView;
		}

		@Override
		public int getCount() {
			return arrayListMenuItems.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}
	}

	class MenuItems {
		String icono;
		String titulo;

		public MenuItems(String icono, String titulo) {
			super();
			this.icono = icono;
			this.titulo = titulo;
		}

		public String getIcono() {
			return icono;
		}

		public void setIcono(String icono) {
			this.icono = icono;
		}

		public String getTitulo() {
			return titulo;
		}

		public void setTitulo(String titulo) {
			this.titulo = titulo;
		}

	}


	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			viewExit();
		}
		return false;
	}

	@SuppressWarnings("deprecation")
	public void showNotification() {
		try {
			String titulo = getString(R.string.sales).toString();
			String Descripcion = getString(R.string.tap_to_open);

			Intent intent = new Intent();
			intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
			intent.setClass(getApplicationContext(), MainActivity.class);

			PendingIntent contentIntent = PendingIntent.getActivity(
					getApplicationContext(), 0, intent, 0);
			Notification notification = new Notification(
					R.drawable.products_on, titulo, System.currentTimeMillis());

			notification.flags |= Notification.FLAG_ONGOING_EVENT;
			notification.setLatestEventInfo(this, titulo, Descripcion,
					contentIntent);
			Actividad.mNM.notify(NOTIFICATION, notification);
			// notifManager.notify(NOTIFICATION, notification);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
